//
//  NSString+GMAdd.m
//  gentleman
//
//  Created by 范月成 on 2017/12/5.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "NSString+GMAdd.h"

@implementation NSString (GMTime)

+ (NSString *)timeIntervalToTimeString:(NSTimeInterval)millisecond {
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSInteger time = (currentTime - millisecond) / 1000;
    if (time < 60 && time >= 0) {
        return @"刚刚";
    } else if (time >= 60 && time < 3600) {
        return [NSString stringWithFormat:@"%ld分钟前", time/60];
    } else if (time >= 3600 && time < 3600 * 24) {
        return [NSString stringWithFormat:@"%ld小时前", time/3600];
    } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
        return [NSString stringWithFormat:@"%ld天前", time/3600/24];
    } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
        return [NSString stringWithFormat:@"%ld个月前", time/3600/24/30];
    } else if (time >= 3600 * 24 * 30 * 12) {
        return [NSString stringWithFormat:@"%ld年前", time/3600/24/30/12];
    } else {
        return @"刚刚";
    }
}

@end

@implementation NSString (GMSubString)

- (NSString *)subStringBetween:(NSString *)startString and:(NSString *)endString{
    if (!self.length) {
        return nil;
    }
    NSRange startRange = [self rangeOfString:startString];
    NSRange endRange = [self rangeOfString:endString];
    NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
    return [self substringWithRange:range];
}

@end

@implementation NSString (GMHandel)

- (NSString *)handleString {
    NSString *prefixRegex = @"^(?:(?:\\([^\\)]*\\))|(?:\\[[^\\]]*\\])|(?:\\{[^\\}]*\\})|(?:~[^~]*~)|\\s+)*";
    NSString *suffixRegex = @"(?:\\s+ch.[\\s\\d-]+)?(?:(?:\\([^\\)]*\\))|(?:\\[[^\\]]*\\])|(?:\\{[^\\}]*\\})|(?:~[^~]*~)|\\s+)*$";
    NSString *newStr = self;
    newStr = [newStr stringByReplacingOccurrencesOfString:[self matchString:newStr toRegexString:prefixRegex].firstObject withString:@""];
    newStr = [newStr stringByReplacingOccurrencesOfString:[self matchString:newStr toRegexString:suffixRegex].firstObject withString:@""];
    //标题内还可能包含"|"
    if ([newStr containsString:@"|"]) {
        NSRange range = [newStr rangeOfString:@"|"];
        newStr = [newStr substringWithRange:NSMakeRange(0, range.location)];
    }
    return newStr;
}

- (NSArray *)matchString:(NSString *)string toRegexString:(NSString *)regexStr {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray * matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    //match: 所有匹配到的字符,根据() 包含级
    NSMutableArray *array = [NSMutableArray array];
    for (NSTextCheckingResult *match in matches) {
        for (int i = 0; i < [match numberOfRanges]; i++) {
            //以正则中的(),划分成不同的匹配部分
            NSString *component = [string substringWithRange:[match rangeAtIndex:i]];
            [array addObject:component];
        }
    }
    return array;
}

+ (NSString *)fileSizeWithByte:(long long)byte {
    return [NSByteCountFormatter stringFromByteCount:byte countStyle:NSByteCountFormatterCountStyleFile];
}

@end
